package com.whjfxx.terminal.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whjfxx.terminal.base.BaseController;
import com.whjfxx.terminal.biz.enums.YesNoEnum;
import com.whjfxx.terminal.biz.param.OrgPageParam;
import com.whjfxx.terminal.biz.param.OrgParam;
import com.whjfxx.terminal.biz.param.OrgTreeParam;
import com.whjfxx.terminal.biz.param.SearchParam;
import com.whjfxx.terminal.biz.service.SysOrganizationService;
import com.whjfxx.terminal.biz.vo.OrgVo;
import com.whjfxx.terminal.commons.Result;
import com.whjfxx.terminal.commons.SelectVo;
import com.whjfxx.terminal.commons.TreeVo;
import com.whjfxx.terminal.entity.SysOrganization;
import com.whjfxx.terminal.exceptions.BizException;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by b.c on 2022/6/7 16:29
 */
@RestController
@Log4j2
@RequestMapping("organization")
public class OrganizationController extends BaseController {

    @Autowired
    SysOrganizationService sysOrganizationService;

    @GetMapping("page")
    public Result<Page<OrgVo>> pageList(SearchParam<OrgPageParam> param) {
        Page<OrgVo> page = sysOrganizationService.pageList(param);
        return Result.success(page);
    }

    @GetMapping("selectList")
    public Result<List<SelectVo<Long, String>>> selectList() {
        LambdaQueryWrapper<SysOrganization> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysOrganization::getStatus, YesNoEnum.YES.status);
        List<SysOrganization> poApps = sysOrganizationService.list(wrapper);
        if (CollectionUtils.isEmpty(poApps)) {
            return Result.success(Collections.emptyList());
        }
        List<SelectVo<Long, String>> selectVos = poApps.stream().map(a -> SelectVo.create(a.getId(), a.getOrganizationName())).collect(Collectors.toList());
        return Result.success(selectVos);
    }


    @PostMapping("save")
    public Result<Object> save(@Validated OrgParam param) {
        if (Objects.isNull(param.getId())) {
            sysOrganizationService.insertSave(param);
        } else {
            sysOrganizationService.updateSave(param);
        }
        return Result.success();
    }

    @PostMapping("disable")
    public Result<Object> disable(@Validated Long id) {
        if (Objects.isNull(id)) {
            throw BizException.build("参数不能为空");
        }
        sysOrganizationService.disable(id);
        return Result.success();
    }

    @PostMapping("enable")
    public Result<Object> enable(@Validated Long id) {
        if (Objects.isNull(id)) {
            throw BizException.build("参数不能为空");
        }
        sysOrganizationService.enable(id);
        return Result.success();
    }

    @GetMapping("tree")
    public Result<List<TreeVo>> tree(OrgTreeParam orgTreeParam) {
       List<TreeVo> treeVos =  sysOrganizationService.tree(orgTreeParam);
       return Result.success(treeVos);
    }
}
